package com.codeoss.framework.webide.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.db.util.ValueNamePairUtil;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.report.def.SummaryReport;
import com.codeoss.framework.report.util.SummaryReportModel;
import com.codeoss.framework.report.util.SummaryReportUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.util.ValueNamePair;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.webide.request.SummaryReportDesignerRequest;
import com.codeoss.framework.webide.response.SummaryReportDesignerResponse;
import com.codeoss.framework.webide.service.ISummaryReportDesignerService;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.service.IIrReportService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class SummaryReportDesignerService implements ISummaryReportDesignerService {
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private SummaryReportUtil summaryReportUtil;
	@Autowired
	private IIrReportService irReportService;
	@Autowired
	private ValueNamePairUtil valueNamePairUtil;
	@Override
	public SummaryReportDesignerResponse initDataSourceConfigPage(String code) {
		// TODO Auto-generated method stub
		SummaryReportDesignerResponse summaryReportDesignerResponse=new SummaryReportDesignerResponse();
		YunContext yunContext=YunContext.getCurrentContext();
		Table modelTable=tableStore.get("ir_model");
		List<ValueNamePair> tableList = valueNamePairUtil.getValueNamePairList(yunContext, modelTable, null, false, true);
		summaryReportDesignerResponse.setTableList(tableList);
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", code);
		IrReport irReport=irReportService.getOne(query);
		String jsonContent=irReport.getJsonContent();
		if(!StringUtils.isEmpty(jsonContent)) {
			SummaryReport summaryReport=summaryReportUtil.buildSummaryReportFromIrReport(irReport);
			Map<String,Object> dataSourceConfigMap=new HashMap<String,Object>();
			summaryReportDesignerResponse.setDataSourceConfig(dataSourceConfigMap);
			
			DataSourceConfig dataSourceConfig=summaryReport.getDataSourceConfig();
			dataSourceConfigMap.put("id", dataSourceConfig.getId());
			dataSourceConfigMap.put("tableName", dataSourceConfig.getTableName());
			dataSourceConfigMap.put("name", dataSourceConfig.getName());
			Expression expression=dataSourceConfig.getWhereClause();
			if(expression!=null) {
				dataSourceConfigMap.put("whereClause", expression.getScript());
			}
			dataSourceConfigMap.put("orderBy", dataSourceConfig.getOrderBy());
		}
		return summaryReportDesignerResponse;
	}
	
	@Override
	public SummaryReportDesignerResponse initSummaryReportConfigPage(String code,String tableName) {
		// TODO Auto-generated method stub
		SummaryReportDesignerResponse summaryReportDesignerResponse=new SummaryReportDesignerResponse();
		Table table = tableStore.get(tableName);
		List<Column> columnList = table.getAllColumnList();
		List<Map<String, Object>> fieldList = new ArrayList<Map<String, Object>>();
		summaryReportDesignerResponse.setFieldList(fieldList);
		for (int i = 0; i < columnList.size(); i++) {
			Column column = columnList.get(i);
			Map<String, Object> fieldMap = new HashMap<String, Object>();
			fieldList.add(fieldMap);
			fieldMap.put("columnName", column.getColumnName());
			fieldMap.put("name", column.getName());
			fieldMap.put("dateType", column.getDataType());
		}
		
		
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", code);
		IrReport irReport=irReportService.getOne(query);
		String jsonContent=irReport.getJsonContent();
		SummaryReport summaryReport=null;
		if(!StringUtils.isEmpty(jsonContent)) {
			 summaryReport=summaryReportUtil.buildSummaryReportFromIrReport(irReport);
			 if(tableName.equals(summaryReport.getDataSourceConfig().getTableName())){
				 summaryReportDesignerResponse.setDimensionRowList(summaryReport.getDimensionRowList());
				 summaryReportDesignerResponse.setDimensionColList(summaryReport.getDimensionColList());
				 summaryReportDesignerResponse.setMeasureList(summaryReport.getMeasureList());
			 }
		}
		return summaryReportDesignerResponse;
	}

	@Override
	public SummaryReportDesignerResponse initReportPreviewPage(SummaryReportDesignerRequest request) {
		// TODO Auto-generated method stub
		SummaryReportDesignerResponse summaryReportDesignerResponse=new SummaryReportDesignerResponse();
		SummaryReport summaryReport=summaryReportUtil.buildSummaryReportFromJson(request.getJson());
		SummaryReportModel summaryReportModel=summaryReportUtil.buildSummaryReportData(summaryReport);
		summaryReportDesignerResponse.setSummaryReportModel(summaryReportModel);
		return summaryReportDesignerResponse;
	}
	
	@Override
	@Transactional
	public SummaryReportDesignerResponse onFinish(SummaryReportDesignerRequest request) {
		// TODO Auto-generated method stub
		SummaryReportDesignerResponse summaryReportDesignerResponse=new SummaryReportDesignerResponse();
		SummaryReport summaryReport=summaryReportUtil.buildSummaryReportFromJson(request.getJson());
		String reportJson=null;
		try {
			reportJson = objectMapper.writeValueAsString(summaryReport);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", request.getCode());
		IrReport irReport=irReportService.getOne(query);
		irReport.setJsonContent(reportJson);
		irReportService.update(irReport);
		return summaryReportDesignerResponse;
	}

}
